import { sysLoginRequest, sysProfileRequest } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
import router from '@/router'

export default {
  // 开启匿名空间

  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setTokenMunanion(state, payload) {
      state.token = payload
      setToken(payload)
    },
    userInfoMunanion(state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    // 登录
    async loginAcnion(context, payload) {
      try {
        const res = await sysLoginRequest(payload)
        console.log(res)
        context.commit('setTokenMunanion', res.data)
        router.push('/')
      } catch (err) {
        console.log(err)
      }
    },
    // 用户个人信息
    async getUserInfoAction(context, payload) {
      try {
        const res = await sysProfileRequest()
        console.log(res)
        context.commit('userInfoMunanion', res.data)
      } catch (error) {
        console.log('用户信息验证失败', error)
      }
    },
    // 验证token
    removertokenAction({ commit }) {
      commit('setTokenMunanion', '')
      commit('userInfoMunanion', {})
    }
  }

}
